package model;

import constants.SMSConstants;
import Repository.*;

public class Imported implements SMSObjectState{

	private final String NAME = "Imported";
	RuleSelector ruleSelector;
	NextStateCalculator stateCalculator;
	Repository repository;
	
	
	
	@Override
	public void execute(SMSObject smsObj) {
		if(smsObj instanceof Lead){
			//System.out.println("Executing Imported State...");
			ruleSelector =  new AssignRuleSelector(smsObj);
			stateCalculator = new NextStateCalculator();
			SMSObjectState newState = stateCalculator.getNextState(smsObj,ruleSelector);
			//System.out.println("New state..." + newState.getStateName());
			
			if(newState.getStateName().equals(NAME)){
				System.out.println("****Lead could not be assigned as it does not comply to the rules****");
				return;
				
			}
		//	System.out.println("Lead" + smsObj.getSmsObjId() +" has been assigned");
			smsObj.setCurrentState(newState);
			repository = RepositoryFactory.getRepository(SMSConstants.LEAD);
			repository.update(smsObj.getSmsObjId(), smsObj);	
			System.out.println("Successfully assigned lead.\nFind assignee details below :");
			System.out.println(smsObj.getManagedByUser());
			
		} else if(smsObj instanceof Opportunity){
			//System.out.println("Executing Imported State...");
			ruleSelector =  new PrepareRuleSelector(smsObj);
			stateCalculator = new NextStateCalculator();
			SMSObjectState newState = stateCalculator.getNextState(smsObj,ruleSelector);
			//System.out.println("New state..." + newState.getStateName());
			if(newState.getStateName().equals(NAME)){
				System.out.println("****Opportunity could not be prepared as it does not pass the rules****");
				return;				
			}
			//System.out.println("Opportunity" + smsObj.getSmsObjId() +" has been prepared");
			smsObj.setCurrentState(newState);
			repository = RepositoryFactory.getRepository(SMSConstants.OPPORTUNITY);
			repository.update(smsObj.getSmsObjId(), smsObj);	
			System.out.println("Successfully prepared opportunity");
		}else if(smsObj instanceof Contract){
			//System.out.println("Executing Imported State...");
			ruleSelector =  new PrepareRuleSelector(smsObj);
			stateCalculator = new NextStateCalculator();
			SMSObjectState newState = stateCalculator.getNextState(smsObj,ruleSelector);
		//	System.out.println("New state..." + newState.getStateName());
			if(newState.getStateName().equals(NAME)){
				System.out.println("****Contract could not be prepared****");
				return;				
			}
			//System.out.println("Opportunity" + smsObj.getSmsObjId() +" has been prepared");
			smsObj.setCurrentState(newState);
			repository = RepositoryFactory.getRepository(SMSConstants.CONTRACT);
			repository.update(smsObj.getSmsObjId(), smsObj);
			System.out.println("Successfully prepared contract and sent for approval.");
			System.out.println("As per the rules, this contract requires "+((Contract)smsObj).getMaxLevel()+" level(s) of approval.");
			
		}
		
	}

	public String getStateName(){
		return NAME;
	}
}
